<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showcloselog" @close="closeLoad" @open="openlist">
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds" v-loading="loading">
      <el-checkbox v-for="item in rolelist" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="submitclick">确定</el-button>
      <el-button size="small">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getUserListAPI, sysroleAPI, AdduserListAPI } from '@/api/empoly'
export default {
  props: {
    showcloselog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    },
    userIDR: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      roleIds: [],
      page: 1,
      pagesize: 999,
      rolelist: [],
      loading: false
    }
  },
  methods: {
    closeLoad() {
      this.$emit('update:showcloselog', false)
    },
    openlist() {
      this.loading = true
      Promise.all([this.roleUserLIST(), this.UserListname()]).then(() => {
        this.loading = false
      })
    },
    async UserListname() {
      const { data: res } = await getUserListAPI({
        page: this.page,
        pagesize: this.pagesize
      })
      console.log(res)
      this.rolelist = res.rows
    },
    async roleUserLIST() {
      console.log(this.userIDR)
      const { data: res } = await sysroleAPI(this.userIDR)
      console.log(res)
      this.roleIds = res.roleIds
    },
    async submitclick() {
      await AdduserListAPI({
        id: this.userIDR,
        roleIds: this.roleIds
      })
      this.$message.success('成功')
      this.closeLoad()
    }
  }
}
</script>
